﻿using System.Web;
using log4net;
using System;
using XProject.Commons.JsonUtil;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace XProject.DataVisit.Handler.View
{
    public abstract class AbstractView : IDisposable
    {

        private HttpContext _context;
        private IJsonRequest _jsonRequest;
        private IJsonResponse _jsonResponse;
        private ILog _log;

        public HttpContext Context
        {
            set
            {
                _context = value;
            }
            get
            {
                return _context;
            }
        }

        public IJsonRequest JsonRequest
        {

            set
            {
                _jsonRequest = value;
            }
            get
            {
                return _jsonRequest;
            }
        }

        public IJsonResponse JsonResponse
        {
            set
            {
                _jsonResponse = value;
            }
            get
            {
                return _jsonResponse;
            }
        }

        public ILog Log
        {
            set
            {
                _log = value;
            }
            get
            {
                return _log;
            }
        }

        private bool _initialized = true;

        public bool Initalized {
            set { this._initialized = value; }
            get { return _initialized; }
        }

        public virtual void init() 
        { 

        }
        /// <summary>
        /// 得到类型转换后的用户提交信息
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public T GetTypedParamObject<T>()
        {
            return (T)JavaScriptUtils.DeserializeObject(JsonRequest.Params.ToString(), typeof(T), MissingMemberHandling.Ignore);
        }

        /// <summary>
        /// 得到类型转换后的用户提交参数信息
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="paramName"></param>
        /// <returns></returns>
        public T GetTypedParamObject<T>(string paramName)
        {
            JToken o = JsonRequest.Params[paramName];
            return (T)JavaScriptUtils.DeserializeObject(o.ToString(), typeof(T), MissingMemberHandling.Ignore);
        }
    
        #region IDisposable 成员

        public virtual void Dispose()
        {
 	        
        }

        #endregion
}
}
